//
//  MHSearchFriendsViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSearchMembersViewModel.h"
#import "MHCommonGroupViewModel.h"

@interface MHSearchMembersViewModel ()
/// searchCommand
@property (nonatomic, readwrite, strong) RACCommand *searchCommand;

@end

@implementation MHSearchMembersViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.info = params[MHViewModelUtilKey];
    }
    return self;
}



- (void)initialize{
    [super initialize];
    @weakify(self);
    self.style = UITableViewStyleGrouped;
    self.interactivePopDisabled = YES;
    self.prefersNavigationBarBottomLineHidden = YES;
//    self.prefersNavigationBarHidden = 
    /// 监听searchText的变化
    [[RACObserve(self, searchText) distinctUntilChanged] subscribeNext:^(NSString * searchText) {
        @strongify(self);
        ///
        if (MHStringIsNotEmpty(searchText)) {
            /// 添加一个数据
        }else{
            self.dataSource = @[];
        }
    }];

    self.searchCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString * searchText) {
        @strongify(self);
        return [[self.services.client fetchCircleMemberPagedListWithCircleId:self.info.autoId roleId:0 pageNum:1 pageSize:self.perPage searchTxt:searchText] doNext:^(NSArray *members) {
            self.circleMembers = [members mutableCopy];
        }];
    }];

    
    
    self.setIdentityCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary  *dataDic ) {
        int memberId = [dataDic[@"memberId"] intValue];
        int identType = [dataDic[@"identType"] intValue];
        return [self.services.client setCircleMemberIdentType:memberId identType:identType];
    }];
    
    self.removeMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client deleteCircleMember: memberId];
    }];
    
    self.blakMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client pullCircleMemberBlack:self.info.autoId userId:memberId];
    }];
    
    self.cancelBlackMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client cancelCircleMemberBlack:memberId];
    }];
    
    self.cancelMemberTypeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client cancelCircleMemberIdentType:memberId];
    }];
    
    RAC(self, circleMembers) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, circleMembers) map:^(NSArray * circleMembers) {
        @strongify(self)
        MHCommonGroupViewModel *group = [MHCommonGroupViewModel groupViewModel];
        group.itemViewModels = [self dataSourceWithcircleMembers:circleMembers];
        self.dataSource = @[group];
        return self.dataSource;
    }];
}

- (NSArray *)dataSourceWithcircleMembers:(NSArray *)circleMembers {
    if (MHObjectIsNil(circleMembers) || circleMembers.count == 0) return nil;
    NSMutableArray *viewModels = [[circleMembers.rac_sequence map:^(MHCircleMember *circleMember) {
        MHCommonItemViewModel *viewModel  = [MHCommonItemViewModel itemViewModelWithTitle:circleMember.nickName   icon:circleMember.fullUserFace];
        viewModel.model = circleMember;
        switch (circleMember.identType) {
            case 1:
                viewModel.subtitle = @"圈主";
                break;
                
            case 2:
                viewModel.subtitle = @"管理员 ...";
                break;
            case 3:
                viewModel.subtitle = @"嘉宾 ...";
                break;
            default:
                viewModel.subtitle = @"";
                break;
        }
        return viewModel;
    }].array mutableCopy];
    
    return viewModels ?: @[] ;
}

@end
